﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace XLB.ExtensionMethod.Net
{
    public class WebHelper
    {

        #region 网络请求

        /// <summary>
        /// Get
        /// </summary>
        /// <param name="headerName">请求头名</param>
        /// <param name="headerValue">请求头值</param>
        /// <param name="Url">请求地址</param>
        /// <param name="timeSpan">超时时间</param>
        /// <returns></returns>
        public static string HttpGet(string headerName, string headerValue, string Url, int timeSpan = 0)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "GET";
            if (!string.IsNullOrEmpty(headerName))
            {
                request.Headers.Add(headerName, headerValue);
            }
            request.ContentType = "text/html;charset=UTF-8";
            request.Timeout = timeSpan > 0 ? timeSpan : 180 * 1000;
            request.ReadWriteTimeout = timeSpan > 0 ? timeSpan : 180 * 1000;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }

        /// <summary>
        /// Post
        /// </summary>
        /// <param name="headerName">请求头名</param>
        /// <param name="headerValue">请求头值</param>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <param name="timeSpan"></param>
        /// <returns></returns>
        public static string HttpPost(string headerName, string headerValue, string Url, string postDataStr, int timeSpan = 0)
        {
            byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(postDataStr);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
            req.ContentType = "application/json;charset=utf-8;";
            req.Method = "POST";
            if (!string.IsNullOrEmpty(headerName))
            {
                req.Headers.Add(headerName, headerValue);
            }
            req.Timeout = timeSpan > 0 ? timeSpan : 180 * 1000;
            req.ReadWriteTimeout = timeSpan > 0 ? timeSpan : 180 * 1000;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();

            Stream stream = res.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            string str = reader.ReadToEnd();

            stream.Close();
            reader.Close();
            return str;
        }
        /*
         
        
        HttpPost("Authorization","eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJnMjExQTZXNUJDRzk3MGhZN3dKUjR5ek9oSUNyQjB5aCIsImV4dGVuZCI6e30sImV4cCI6MTcxNDEwNjA3Mn0.xr2eGsdthRBtyUXoijBo4pP8HYJzE4hnf5CbztXLm3XvraOi-c0WsTJ8GezXwvJR4rD1oK5XM2q8QPh6nPOGFA","http://www.baod.com","{"sd":"sd"}",10)
         
         
         */

        #endregion

    }
}
